<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${pkg}.dao.Base${upTumTabNam}Dao">
	<resultMap id="BaseResultMap" type="${pkg}.bean.${upTumTabNam}Bean">
		<#list columns as col>
			<#if col.columnKey == "PRI">
		<id column="${col.columnName}" property="${col.lowJavaName}" />
			<#else>
		<result column="${col.columnName}" property="${col.lowJavaName}" />
			</#if>
		</#list>
	</resultMap>
	<resultMap id="hashMap" type="java.util.HashMap">
	</resultMap>

	<sql id="Base_Column_List">
		<#list columns as col>
		${col.columnName}<#if col_has_next>, </#if>			
		</#list>
	</sql>
	<#if hasPk>
	
	<sql id="Primary_Key_List">
		<#list pkList as pk>${pk.columnName}<#if pk_has_next>, </#if></#list>
	</sql>
	</#if>

	<select id="select" parameterType="${pkg}.bean.${upTumTabNam}Bean"
		resultMap="BaseResultMap">
		SELECT
		<include refid="Base_Column_List" />
		FROM ${tabNam} WHERE 1=1 
		<#list columns as col>
			<#if col.javaType == "String"><if test="${col.lowJavaName} != null<#if col.isString> and ${col.lowJavaName} != ''</#if>">
				AND ${col.columnName} = #${"\{"}${col.lowJavaName}}
			</if>
			<#elseif col.javaType == "java.sql.Timestamp"><if test="${col.lowJavaName}Max != null">
				AND ${col.columnName} &lt;= #${"\{"}${col.lowJavaName}Max}
			</if>
			<if test="${col.lowJavaName}Min != null">
				AND ${col.columnName} &gt;= #${"\{"}${col.lowJavaName}Min}
			</if>
			<#elseif col.javaType == "java.sql.Date"><if test="${col.lowJavaName}Max != null">
				AND ${col.columnName} &lt;= #${"\{"}${col.lowJavaName}Max}
			</if>
			<if test="${col.lowJavaName}Min != null">
				AND ${col.columnName} &gt;= #${"\{"}${col.lowJavaName}Min}
			</if>
			<if test="${col.lowJavaName} != null">
				AND ${col.columnName} = #${"\{"}${col.lowJavaName}}
			</if></#if>
		</#list>
		<#if hasPk>
		ORDER BY
		<include refid="Primary_Key_List" />
		</#if>
	</select>
	<#if hasPk>

	<select id="selectByPK" parameterType="${pkg}.bean.${upTumTabNam}Bean"
		resultMap="BaseResultMap">
		SELECT
		<include refid="Base_Column_List" />
		FROM ${tabNam}
		WHERE 
		<#list pkList as pk>
		${pk.columnName} = #${"\{"}${pk.lowJavaName}}<#if pk_has_next> AND </#if>
		</#list>
		ORDER BY
		<include refid="Primary_Key_List" />
	</select>
	</#if>
	<#if hasPk>

	<delete id="delete" parameterType="${pkg}.bean.${upTumTabNam}Bean">
		DELETE FROM ${tabNam}
		WHERE
		<#list pkList as pk>
		${pk.columnName} = #${"\{"}${pk.lowJavaName}}<#if pk_has_next> AND </#if>
		</#list>
	</delete>
	</#if>

	<delete id="deletes" parameterType="${pkg}.bean.${upTumTabNam}Bean">
		DELETE FROM ${tabNam}
		WHERE 1=1 
		<#list columns as col>	
		<#if col.isString><if test="${col.lowJavaName} != null and ${col.lowJavaName} != ''">
			AND ${col.columnName} = #${"\{"}${col.lowJavaName}}
		</if></#if>
		</#list>
	</delete>

	<insert id="insert" parameterType="${pkg}.bean.${upTumTabNam}Bean">
		INSERT INTO ${tabNam}
		<trim prefix="(" suffix=")" suffixOverrides=",">		
		<#list columns as col>
			<if test="${col.lowJavaName} != null">
				${col.columnName},
			</if>		
		</#list>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
		<#list columns as col>
			<if test="${col.lowJavaName} != null">
				#${"\{"}${col.lowJavaName}},
			</if>	
		</#list>
		</trim>
	</insert>

	<update id="updates">
		UPDATE ${tabNam}
		<set>
		<#list columns as col>
			<if test="value.isInit${col.upJavaName}">
				<choose>
					<when test="value.${col.lowJavaName} != null">
						${col.columnName} = #${"\{"}value.${col.lowJavaName}},
					</when>
					<otherwise>
						${col.columnName} = NULL,
					</otherwise>
				</choose>
			</if>
		</#list>
		</set>
		WHERE 1=1 
		<#list columns as col>
		<#if col.isString><if test="condition.${col.lowJavaName} != null and condition.${col.lowJavaName} != ''">
			AND ${col.columnName} =	#${"\{"}condition.${col.lowJavaName}} 
		</if></#if>
		</#list>
	</update>
	<#if hasPk>

	<update id="update" parameterType="${pkg}.bean.${upTumTabNam}Bean">
		UPDATE ${tabNam}
		<set>
		<#list columns as col>
			<if test="isInit${col.upJavaName}">
				<choose>
					<when test="${col.lowJavaName} != null">
						${col.columnName} = #${"\{"}${col.lowJavaName}},
					</when>
					<otherwise>
						${col.columnName} = NULL,
					</otherwise>
				</choose>
			</if>
		</#list>
		</set>
		WHERE 
		<#list pkList as pk>
		${pk.columnName} = #${"\{"}${pk.lowJavaName}}<#if pk_has_next> AND </#if>
		</#list>
	</update>	
	</#if>

	<select id="selectPage" resultMap="BaseResultMap">
		SELECT 
		<include refid="Base_Column_List" />
		FROM
		${tabNam} WHERE 1=1
		<if test="${lowTumTabNam}!=null">
			<#list columns as col>
			<#if col.javaType == "String"><if test="${lowTumTabNam}.${col.lowJavaName} != null<#if col.isString> and ${lowTumTabNam}.${col.lowJavaName} != ''</#if>">
				AND ${col.columnName} = #${"\{"}${lowTumTabNam}.${col.lowJavaName}}
			</if>
			<#elseif col.javaType == "java.sql.Timestamp"><if test="${lowTumTabNam}.${col.lowJavaName}Max != null">
				AND ${col.columnName} &lt;= #${"\{"}${lowTumTabNam}.${col.lowJavaName}Max}
			</if>
			<if test="${lowTumTabNam}.${col.lowJavaName}Min != null">
				AND ${col.columnName} &gt;= #${"\{"}${lowTumTabNam}.${col.lowJavaName}Min}
			</if>
			<#elseif col.javaType == "java.sql.Date"><if test="${lowTumTabNam}.${col.lowJavaName}Max != null">
				AND ${col.columnName} &lt;= #${"\{"}${lowTumTabNam}.${col.lowJavaName}Max}
			</if>
			<if test="${lowTumTabNam}.${col.lowJavaName}Min != null">
				AND ${col.columnName} &gt;= #${"\{"}${lowTumTabNam}.${col.lowJavaName}Min}
			</if>
			<if test="${lowTumTabNam}.${col.lowJavaName} != null">
				AND ${col.columnName} = #${"\{"}${lowTumTabNam}.${col.lowJavaName}}
			</if></#if>
			</#list>
		</if>
		ORDER BY
		<include refid="Primary_Key_List" />
		LIMIT #${"\{"}offset},#${"\{"}pageSize}
	</select>
	
	<select id="selectTotal" parameterType="${pkg}.bean.${upTumTabNam}Bean" resultType="Integer">
		SELECT COUNT(*) TOTALRECORD FROM ${tabNam}
		WHERE 1=1
		<if test="_parameter!=null">
			<#list columns as col>
			<#if col.javaType == "String"><if test="${col.lowJavaName} != null<#if col.isString> and ${col.lowJavaName} != ''</#if>">
				AND ${col.columnName} = #${"\{"}${col.lowJavaName}}
			</if>
			<#elseif col.javaType == "java.sql.Timestamp"><if test="${col.lowJavaName}Max != null">
				AND ${col.columnName} &lt;= #${"\{"}${col.lowJavaName}Max}
			</if>
			<if test="${col.lowJavaName}Min != null">
				AND ${col.columnName} &gt;= #${"\{"}${col.lowJavaName}Min}
			</if>
			<#elseif col.javaType == "java.sql.Date"><if test="${col.lowJavaName}Max != null">
				AND ${col.columnName} &lt;= #${"\{"}${col.lowJavaName}Max}
			</if>
			<if test="${col.lowJavaName}Min != null">
				AND ${col.columnName} &gt;= #${"\{"}${col.lowJavaName}Min}
			</if>
			<if test="${col.lowJavaName} != null">
				AND ${col.columnName} = #${"\{"}${col.lowJavaName}}
			</if></#if>
			</#list>
		</if>
	</select>
	
	<insert id="insertBatch" parameterType="java.util.List">
		INSERT INTO ${tabNam}(
		<include refid="Base_Column_List" />
		) VALUES
		<foreach collection="list" item="item" index="index"
			separator=",">
		<trim prefix="(" suffix=")" suffixOverrides=",">
		<#list columns as col>
			#${"\{"}item.${col.lowJavaName}},		
		</#list>
		</trim>
		</foreach>
	</insert>

	<select id="selectBySql" parameterType="java.lang.String"
		resultMap="hashMap">
		$${"\{"}_parameter}
	</select>

	<select id="selectTotalBySql" parameterType="java.lang.String"
		resultType="Integer">
		$${"\{"}_parameter}
	</select>
</mapper>  